package servidorVehiculo.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import Dominio.Medida;
import Dominio.Sensor;



public class SensorDAO {
	public static SensorDAO instance;
	public Connection connection;
	
	public static void init() throws SQLException{
		instance = new SensorDAO();
		instance.connection = DAOServidorVehiculo.getConnection();
	}
	
	public static void close(){
		instance = null;
	}
	
	
	
	public static List<Sensor> listSensores() throws SQLException{
		return instance.listaSensores();
	}
	
	public static Sensor getSensor(int id) throws SQLException{
		return instance.devolverSensor(id);
	}
	
	public static void updateSensor(int id, boolean on) throws SQLException{
		instance.actualizarSensor(id, on);
	}
	
	public static void deleteSensor(int id) throws SQLException{
		instance.quitarSensor(id);
	}
	
	public static void insertSensor(String desc, boolean on) throws SQLException{
		instance.introducirSensor(desc, on);
	}
	
	public static void insertMedida(int id, Date fecha, String lat, String lon, int valor) throws SQLException{
		instance.introducirMedida(id, fecha, lat, lon, valor);
	}
	
	// mete un sensor en el vehiculo
	public void introducirSensor(String desc, boolean on)
			throws SQLException {
		PreparedStatement smt = connection.prepareStatement("INSERT INTO SENSOR(DESCRIPCION, SENSORON) VALUES (?,?)");
		smt.setString(1, desc);
		smt.setBoolean(2, on);
		smt.executeUpdate();
		smt.close();
	}

	// quita un sensor del vehiculo
	public void quitarSensor(int id) throws SQLException {
		PreparedStatement smt = connection.prepareStatement("DELETE FROM SENSOR WHERE ID = ?");
		smt.setInt(1, id);
		smt.executeUpdate();
		smt.close();
	}

	// cambia el estado del sensor
	public void actualizarSensor(int id, boolean on) throws SQLException {
		PreparedStatement smt = connection.prepareStatement("UPDATE SENSOR SET SENSORON = ? WHERE ID = ?");
		smt.setBoolean(1, on);
		smt.setInt(2, id);
		smt.executeUpdate();
		smt.close();
	}

	// devuelve un sensor del vehiculo con sus medidas
	public Sensor devolverSensor(int id) throws SQLException {


		PreparedStatement smt = connection.prepareStatement("SELECT * FROM SENSOR WHERE ID = ?");
		smt.setInt(1, id);
		ResultSet rs = smt.executeQuery();
		rs.next();

		List<Medida> lm = new ArrayList<Medida>();
		lm = getMedidas(id);

		Sensor s = new Sensor(rs.getInt(1), rs.getString(2), rs.getBoolean(3), lm);

		rs.close();
		smt.close();

		return s;
	}
	// devuelve la lista de sensores del vehiculo
	public List<Sensor> listaSensores() throws SQLException {

		List<Sensor> lSensores = new ArrayList<Sensor>();

		PreparedStatement stm = connection.prepareStatement("SELECT * FROM SENSOR");
		ResultSet rs = stm.executeQuery();
		while (rs.next()) {

			int id = rs.getInt(1);
			String des = rs.getString(2);
			boolean on = rs.getBoolean(3);
			
			List<Medida>lmed = new ArrayList<Medida>();
			lmed = getMedidas(id);
			
			Sensor s = new Sensor(id, des, on, lmed);
			lSensores.add(s);
		}
		rs.close();
		stm.close();

		return lSensores;
	}
	
	public List<Medida> getMedidas(int id) throws SQLException{
		PreparedStatement stm = connection.prepareStatement(("SELECT * FROM MEDIDA WHERE ID_SENSOR = ?"));
		stm.setInt(1, id);
		ResultSet rs = stm.executeQuery();
		List<Medida> lm = new ArrayList<Medida>();
		while(rs.next()){
			Medida m = new Medida(rs.getTimestamp(5), rs.getString(3), rs.getString(4), rs.getInt(2));
			lm.add(m);
		}
		
		rs.close();
		stm.close();
		
		return lm;	
	}
	
	public void introducirMedida(int id, Date fecha, String lat, String lon, int valor) throws SQLException{
		PreparedStatement stm = connection.prepareStatement(("INSERT INTO MEDIDA(VALOR, LATITUD, LONGITUD, FECHA, ID_SENSOR) VALUES (?,?,?,?,?)"));
		stm.setInt(1, valor);
		stm.setString(2, lat);
		stm.setString(3, lon);
		stm.setDate(4, new java.sql.Date(fecha.getTime()));
		stm.setInt(5, id);
		stm.executeUpdate();
		stm.close();
	}
	


}
